package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.EmailNotifications;
import com.example.mall.dto.EmailNotificationsDto;
import com.example.mall.mapper.EmailNotificationsMapper;
import com.example.mall.query.EmailNotificationsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.IEmailNotificationsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 邮件通知表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class EmailNotificationsServiceImpl extends ServiceImpl<EmailNotificationsMapper, EmailNotifications> implements IEmailNotificationsService {

    @Autowired
    private EmailNotificationsMapper emailNotificationsMapper;

    @Override
    public PageDTO<EmailNotificationsDto> queryPage(EmailNotificationsQuery emailNotificationsQuery) {
        if (emailNotificationsQuery == null) {
            emailNotificationsQuery = new EmailNotificationsQuery();
        }
        Page<EmailNotifications> page = emailNotificationsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<EmailNotifications> cartPage = lambdaQuery()
                .like(emailNotificationsQuery.getEmailId()!= null, EmailNotifications::getEmailId, emailNotificationsQuery.getEmailId())
                .like(emailNotificationsQuery.getUserId()!= null, EmailNotifications::getUserId, emailNotificationsQuery.getUserId())
                .like(emailNotificationsQuery.getTypeId()!= null, EmailNotifications::getTypeId, emailNotificationsQuery.getTypeId())
                .like(emailNotificationsQuery.getEmailAddress()!= null, EmailNotifications::getEmailAddress, emailNotificationsQuery.getEmailAddress())
                .like(emailNotificationsQuery.getSubject()!= null, EmailNotifications::getSubject, emailNotificationsQuery.getSubject())
                .like(emailNotificationsQuery.getContent()!= null, EmailNotifications::getContent, emailNotificationsQuery.getContent())
                .like(emailNotificationsQuery.getStatus()!= null, EmailNotifications::getStatus, emailNotificationsQuery.getStatus())
                .orderByDesc(EmailNotifications::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, EmailNotificationsDto.class);
    }

    @Override
    public EmailNotifications queryEmailNotificationsById(Long emailId) {
        AssertUtil.isNotNull(emailId, "emailId不能为空");
        return emailNotificationsMapper.selectById(emailId);
    }

    @Override
    public List<EmailNotifications> queryEmailNotificationsList() {
        return this.list();
    }

    @Override
    public void insert(EmailNotificationsDto emailNotificationsDto) {
        EmailNotifications emailNotifications = new EmailNotifications();
        CopyUtils.copyProperties(emailNotificationsDto, emailNotifications);
        emailNotifications.setEmailId(CodeGenerateUtils.generateUniqueId());
        emailNotifications.setCreateDate(new Date());
        emailNotifications.setUpdateDate(new Date());
        emailNotifications.setStatus(BaseConstants.EFFECTIVE_STATUS);
        emailNotificationsMapper.insert(emailNotifications);
    }

    @Override
    public void update(EmailNotificationsDto emailNotificationsDto) {
        AssertUtil.isNotNull(emailNotificationsDto.getEmailId(), "emailId不能为空");
        EmailNotifications emailNotifications = emailNotificationsMapper.selectById(emailNotificationsDto.getEmailId());
        AssertUtil.isNotNull(emailNotifications, "emailId不存在");
        CopyUtils.copyProperties(emailNotificationsDto, emailNotifications);
        emailNotifications.setUpdateDate(new Date());
        emailNotificationsMapper.updateById(emailNotifications);
    }

    @Override
    public void delete(Long emailId) {
        AssertUtil.isNotNull(emailId, "emailId不能为空");
        EmailNotifications emailNotifications = emailNotificationsMapper.selectById(emailId);
        AssertUtil.isNotNull(emailNotifications, "emailId不存在");
        emailNotifications.setStatus(BaseConstants.LOSE_STATUS);
        emailNotifications.setUpdateDate(new Date());
        emailNotificationsMapper.updateById(emailNotifications);
    }

    @Override
    public void deleteBatch(List<Long> emailIds) {
        AssertUtil.isNotNull(emailIds, "emailIds不能为空");
        List<EmailNotifications> emailNotifications = emailNotificationsMapper.selectBatchIds(emailIds);
        AssertUtil.isNotNull(emailNotifications, "emailIds不存在");
        for (EmailNotifications emailNotification : emailNotifications) {
            emailNotification.setStatus(BaseConstants.LOSE_STATUS);
            emailNotification.setUpdateDate(new Date());
            emailNotificationsMapper.updateById(emailNotification);
        }
    }
}
